import * as mobx from 'mobx';
import {observable, action, useStrict, computed, asMap, toJS} from 'mobx';
import {Tools, Tabs, Toast} from "weaver-mobile";
import {getTamperDatas, checkProtectDatas} from "../apis/secondAuth";
import * as authTypes from "../constants/secondAuthType";
import {suppoertCAAuth, getCAAuthInfo} from "../util/CAJsApi";

const getLabel = Tools.getLabel;

export class SecondAuthDetail {
    /**
     *  流程的一些参数
     */
    commonParam = {};

    /**
     * 查看篡改信息详情窗口的tab
     */
    @observable
    tabs = [];

    /**
     * tab被选中的key
     */
    @observable
    selectedKey = "";

    /**
     * 证书信息tab页的数据
     */
    @observable
    certInfo = [];

    /**
     * 篡改信息tab页的数据
     */
    @observable
    tamperInfo = {};

    /**
     * 数据保护的类型
     */
    protectType = 0;

    /**
     *  二次验证的结果
     */
    @observable
    checkResult = observable.map ? observable.map({}) : asMap({});

    constructor() {
        this.loadAuthDetail = this.loadAuthDetail.bind(this);
        this.setCAAuthInfo = this.setCAAuthInfo.bind(this);
        this.checkProtectDatas = this.checkProtectDatas.bind(this);
        this.changeTab = this.changeTab.bind(this);
        this.setCommonParam = this.setCommonParam.bind(this);
        this.setCheckResult = this.setCheckResult.bind(this);
    }

    @action("加载篡改的详细信息")
    loadAuthDetail(logid = -1) {
        this.tabs = [];
        this.selectedKey = "tamperInfo";

        const {requestid, workflowid, nodeid, formid, isbill} = this.commonParam;
        const requestParam = {requestid, workflowid, nodeid, formid, isbill, logid};

        getTamperDatas(requestParam).then(data => {
            const {success} = data;
            if (success == "1") {
                const {certInfo, tamperInfo, protectType} = data;
                this.protectType = protectType;

                this.certInfo = [...certInfo];
                if (protectType == authTypes.QYS) {      //保护数据方式：契约锁，返回证书信息
                    if (certInfo.length > 0) {
                        this.tabs.push({tabKey: "certInfo", title: getLabel(501463, "证书信息")});
                    }
                } else if (protectType == authTypes.CA_AUTH) {    //保护数据方式：CA，从客户端中获取证书信息
                    if (suppoertCAAuth()) {
                        getCAAuthInfo(data => this.setCAAuthInfo(data));
                        this.tabs.push({tabKey: "certInfo", title: getLabel(501463, "证书信息")});
                    }
                }

                const checkResult0 = tamperInfo.checkResult;
                this.tamperInfo = mobx.observable.map(tamperInfo);
                let title = getLabel(501477, "篡改信息");
                if (checkResult0 === "1") {     //保护数据未被篡改，显示受保护数据
                    title = getLabel(506277, "受保护数据");
                }

                this.tabs.push({tabKey: "tamperInfo", title});
            }
        });
    }

    setCAAuthInfo(data) {
        const errCode = data.errCode;
        const errMsg = data.errMsg;
        const caCertInfo = data.getCACertInfo;

        if (errCode == 0) {       //成功
            if (caCertInfo) {
                const {owner, startTime, endTime, signAlg} = caCertInfo;
                this.certInfo.push({label: getLabel(79, "所有者"), value: owner});
                this.certInfo.push({label: getLabel(501478, "颁发时间"), value: startTime});
                this.certInfo.push({label: getLabel(501479, "失效时间"), value: endTime});
                this.certInfo.push({label: getLabel(32344, "加密算法"), value: signAlg});
            }
        } else {
            if (!!errMsg) {
                Toast.fail(errMsg, 2);
            } else {
                Toast.fail(getLabel(501213, "获取CA证书信息失败"), 2);
            }
        }
    }

    @action("点击签字意见列表验证被保护的数据")
    checkProtectDatas(logId = -1) {
        this.tabs = [];
        this.selectedKey = "tamperInfo";

        const {requestid, workflowid, nodeid, formid, isbill} = this.commonParam;
        const requestParam = {
            requestId: requestid,
            workflowId: workflowid,
            nodeId: nodeid,
            formId: formid,
            isbill,
            logId
        };

        return checkProtectDatas(requestParam);
    }

    @action("切换tab")
    changeTab(tkey) {
        this.selectedKey = tkey;
    }

    @action("设置流程里的一些参数")
    setCommonParam(commonParam = {}) {
        this.commonParam = commonParam;
    }

    @action("设置验证的结果")
    setCheckResult(logId = "-1", result = {}) {
        let thatResult = {};
        if (this.checkResult.has(logId)) {
            thatResult = this.checkResult.get(logId)
        }

        this.checkResult.set(logId, {...thatResult, ...result});
    }
}